package com.ruoyi.user;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.incident.domain.XlkjIncident;
import com.ruoyi.system.mapper.SysUserMapper;
import com.ruoyi.system.mapper.SysUserRoleMapper;
import com.ruoyi.utils.UrlUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/app/user")
public class UserControllerApp extends BaseController {

    @Autowired
    private SysUserMapper sysUserMapper;
    @Autowired
    private SysUserRoleMapper sysUserRoleMapper;

    @GetMapping("/{username}")
    public AjaxResult getNickName(@PathVariable("username") String username)
    {
        SysUser sysUser = sysUserMapper.selectUserByUserName(username);
        if(sysUser != null && !sysUser.getAvatar().equals("")){
            sysUser.setAvatar(UrlUtils.getUrl() + sysUser.getAvatar());
        }
        return AjaxResult.success(sysUser);
    }

    /**
     * 通讯录（不查询普通用户的）
     * @return
     */
    @GetMapping("/list")
    public TableDataInfo list(){
        //筛查
        Long[] longs = sysUserRoleMapper.selectUserIdByRoleId();
        SysUser sysUser = new SysUser();
        sysUser.setLongs(longs);
        List<SysUser> sysUsers = sysUserMapper.selectUserList(sysUser);
        for(SysUser user:sysUsers){
            if(user.getAvatar() != null && !user.getAvatar().equals("")){
                user.setAvatar(UrlUtils.getUrl() + user.getAvatar());
            }
        }
        return getDataTable(sysUsers);
    }

}
